<?php
class ImageComponent extends Object {
	function upload_image_and_thumbnail($data, $datakey, $imgscale, $thumbscale, $folderName, $square) {
		if (strlen($data['Picture'][$datakey]['name']) > 4) {
			$error = 0;
			$tempuploaddir = IMG_PATH."/temp";
			$biguploaddir = IMG_PATH."/" . $folderName ;
			$smalluploaddir = IMG_PATH."/" . $folderName . "/thumb";

			if (!is_dir($tempuploaddir)) mkdir($tempuploaddir, true);
			if (!is_dir($biguploaddir)) mkdir($biguploaddir, true);
			if (!is_dir($smalluploaddir)) mkdir($smalluploaddir, true);

			$filetype = $this->getFileExtension($data['Picture'][$datakey]['name']);
			$filetype = strtolower($filetype);

			if (($filetype != "jpeg") && ($filetype != "jpg") && ($filetype != "gif") && ($filetype != "png")) {
				return;
			} else {
				$imgsize = getimagesize($data['Picture'][$datakey]['tmp_name']);
			}

			$id_unic = str_replace(".", "", strtotime("now"));
			$filename = $id_unic;

			settype($filename, "string");
			$filename .= ".";
			$filename .= $filetype;
			$tempfile = $tempuploaddir . "/$filename";
			$resizedfile = $biguploaddir . "/$filename";
			$croppedfile = $smalluploaddir . "/$filename";


			if (is_uploaded_file($data['Picture'][$datakey]['tmp_name'])) {
				if (!copy($data['Picture'][$datakey]['tmp_name'], "$tempfile")) {
					print "Error Uploading File!";
					exit();
				} else {
					$this->resize_img($tempfile, $imgscale, $resizedfile);

					if ($square) {
						$this->crop_img($tempfile, $thumbscale, $croppedfile);
					} else {
						$this->resize_img($tempfile, $thumbscale, $croppedfile);
					}

					@chmod($tempfile, 0777);
					@unlink($tempfile);
				}
			}

			return $filename;
		}
	}

	function delete_image($filename, $folderName) {
		@chmod("img/" . $folderName . "/big/" . $filename, 0777);
		@unlink("img/" . $folderName . "/big/" . $filename);
		@chmod("img/" . $folderName . "/small/" . $filename, 0777);
		@unlink("img/" . $folderName . "/small/" . $filename);
	}

	function crop_img($imgname, $scale, $filename) {
		$filetype = $this->getFileExtension($imgname);
		$filetype = strtolower($filetype);

		switch ($filetype) {
			case "jpeg":
			case "jpg": {
				$img_src = imagecreatefromjpeg($imgname);
				break;
			}
			case "gif": {
				$img_src = imagecreatefromgif($imgname);
				break;
			}
			case "png": {
				$img_src = imagecreatefrompng($imgname);
				break;
			}
		}

		$width = imagesx($img_src);
		$height = imagesy($img_src);
		$ratiox = $width / $height * $scale;
		$ratioy = $height / $width * $scale;

		$newheight = ($width <= $height) ? $ratioy : $scale;
		$newwidth = ($width <= $height) ? $scale : $ratiox;

		$cropx = ($newwidth - $scale != 0) ? ($newwidth - $scale) / 2 : 0;
		$cropy = ($newheight - $scale != 0) ? ($newheight - $scale) / 2 : 0;

		$resampled = imagecreatetruecolor($newwidth, $newheight);
		$cropped = imagecreatetruecolor($scale, $scale);

		imagecopyresampled($resampled, $img_src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
		imagecopy($cropped, $resampled, 0, 0, $cropx, $cropy, $newwidth, $newheight);

		switch ($filetype) {
			case "jpeg":
			case "jpg": {
				imagejpeg($cropped, $filename, 80);
				break;
			}
			case "gif": {
				imagegif($cropped, $filename, 80);
				break;
			}
			case "png": {
				imagepng($cropped, $filename, 80);
				break;
			}
		}
	}

	function resize_img($imgname, $size, $filename)	{
		$filetype = $this->getFileExtension($imgname);
		$filetype = strtolower($filetype);

		switch ($filetype) {
			case "jpeg":
			case "jpg": {
				$img_src = imagecreatefromjpeg($imgname);
				break;
			}
			case "gif": {
				$img_src = imagecreatefromgif($imgname);
				break;
			}
			case "png": {
				$img_src = imagecreatefrompng($imgname);
				break;
			}
		}

		$true_width = imagesx($img_src);
		$true_height = imagesy($img_src);

		if ($true_width >= $true_height)
		{
			$width = $size;
			$height = ($width / $true_width) * $true_height;
		} else {
			$width = $size;
			$height = ($width / $true_width) * $true_height;
		}

		$img_des = imagecreatetruecolor($width, $height);
		imagecopyresampled($img_des, $img_src, 0, 0, 0, 0, $width, $height, $true_width, $true_height);

		switch ($filetype) {
			case "jpeg":
			case "jpg": {
				imagejpeg($img_des, $filename, 80);
				break;
			}
			case "gif": {
				imagegif($img_des, $filename, 80);
				break;
			}
			case "png": {
				imagepng($img_des, $filename, 80);
				break;
			}
		}
	}

	function getFileExtension($str) {
		$i = strrpos($str, ".");

		if (!$i) {
			return "";
		}

		$l = strlen($str) - $i;
		$ext = substr($str, $i + 1, $l);
		return $ext;
	}
}
?>